package com.components.project.ui.base

import android.view.View
import androidx.annotation.StringRes
import com.components.base.action.TitleBarAction
import com.components.base.titlebar.TitleBar
import com.components.base.ui.activity.BaseActivity
import com.gyf.immersionbar.ImmersionBar

abstract class ProjectBaseActivity : BaseActivity(), TitleBarAction {

    /**
     * 标题栏对象
     */
    private var mTitleBar: TitleBar? = null

    /**
     * 状态栏沉浸
     */
    private var mImmersionBar: ImmersionBar? = null

    override fun initLayout() {
        super.initLayout()
        titleBar()?.setOnTitleBarListener(this)

        // 初始化沉浸式状态栏
        if (isStatusBarEnabled()) {
            getStatusBarConfig()?.init()

            // 设置标题栏沉浸
            if (titleBar() != null) {
                ImmersionBar.setTitleBar(this, titleBar())
            }
        }
    }

    /**
     * 是否使用沉浸式状态栏
     */
    protected fun isStatusBarEnabled(): Boolean {
        return true
    }

    /**
     * 状态栏字体深色模式
     */
    protected fun isStatusBarDarkFont(): Boolean {
        return true
    }

    /**
     * 初始化沉浸式状态栏
     */
    protected fun createStatusBarConfig(): ImmersionBar {
        return ImmersionBar.with(this) // 默认状态栏字体颜色为黑色
            .statusBarDarkFont(isStatusBarDarkFont())
    }

    /**
     * 获取状态栏沉浸的配置对象
     */
    fun getStatusBarConfig(): ImmersionBar? {
        if (mImmersionBar == null) {
            mImmersionBar = createStatusBarConfig()
        }
        return mImmersionBar
    }

    /**
     * 设置标题栏的标题
     */
    override fun setTitle(@StringRes id: Int) {
        setTitle(getString(id))
    }

    /**
     * 设置标题栏的标题
     */
    override fun setTitle(title: CharSequence) {
        super<TitleBarAction>.setTitle(title)
        if (titleBar() != null) {
            titleBar()?.setTitle(title)
        }
    }

    override fun titleBar(): TitleBar? {
        if (mTitleBar == null) {
            mTitleBar = obtainTitleBar(contentView)
        }
        return mTitleBar
    }

    override fun onLeftClick(v: View) {
        onBackPressed()
    }

}